package xin.petstore.framework.rpc.dubbo;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.rpc.service.GenericService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;
import xin.petstore.framework.utils.JsonMapper;

import java.util.Map;

public class DubboDirectClient {
    private static Logger logger = LoggerFactory.getLogger(DubboDirectClient.class);
    private ReferenceConfig<GenericService> reference;
    private String applicationConfigName;

    public DubboDirectClient() {
        ApplicationConfig application = new ApplicationConfig();
        application.setName(this.applicationConfigName);
        this.reference = new ReferenceConfig();
        this.reference.setApplication(application);
    }

    public String getApplicationConfigName() {
        return this.applicationConfigName;
    }

    public void setApplicationConfigName(String applicationConfigName) {
        this.applicationConfigName = applicationConfigName;
    }

    public String multipleParsInvoke(String url, String interfaceName, String methodName, String version, String group, String[] parClass, Object[] pars) {
        Assert.notNull(url, "url is not null");
        Assert.notNull(interfaceName, "interfaceName is not null");
        Assert.notNull(methodName, "methodName is not null");
        try {
            this.reference.setUrl(url);
            this.reference.setInterface(interfaceName);
            if (StringUtils.isNotEmpty(version))
                this.reference.setVersion(version);
            if (StringUtils.isNotEmpty(group))
                this.reference.setGroup(group);
            GenericService genericService = (GenericService) this.reference.get();
            Object result = genericService.$invoke(methodName, parClass, pars);
            return JsonMapper.buildNormalMapper().toJson(result);
        } catch (Exception e) {
            logger.error("invoke method error \n", e);
        }
        return null;
    }

    public String singleParsInvoke(String url, String interfaceName, String methodName, String version, String group, String parClass, Map<String, Object> pars) {
        Assert.notNull(url, "url is not null");
        Assert.notNull(interfaceName, "interfaceName is not null");
        Assert.notNull(methodName, "methodName is not null");
        try {
            this.reference.setUrl(url);
            this.reference.setInterface(interfaceName);

            if (StringUtils.isNotEmpty(version)) {
                this.reference.setVersion(version);
            }
            if (StringUtils.isNotEmpty(group)) {
                this.reference.setGroup(group);
            }
            GenericService genericService = (GenericService) this.reference.get();
            Object result = genericService.$invoke(methodName, new String[]{parClass}, new Object[]{pars});
            return JsonMapper.buildNormalMapper().toJson(result);
        } catch (Exception e) {
            logger.error("invoke method error \n", e);
        }
        return null;
    }
}
